<template>
  <div class="dashboard-editor-container">

    <el-row :gutter="40" class="panel-group" style="margin-left: 16px">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-shopping">
            <svg-icon icon-class="shopping" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              总订单量
            </div>
            <count-to :start-val="0" :end-val="orderTotalNum" :duration="3600" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-money">
            <svg-icon icon-class="money" class-name="card-panel-icon" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              总利润
            </div>
            <count-to :start-val="0" :end-val="totalProfit" :duration="3200" class="card-panel-num" />
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-date-picker
        style="margin-left: 16px;"
        v-model="currentMonth"
        type="month"
        value-format="yyyy-MM"
        format="yyyy-MM"
        @change="queryMonthOrder"
        placeholder="选择月">
      </el-date-picker>
    </el-row>
    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <el-card>
        <ve-line :data="lineChartData" :settings="chartSettings"></ve-line>
      </el-card>
    </el-row>
  </div>
</template>

<script>
  import {getOrderStat,getOrderChartData} from '@/api/wx/order'
  import CountTo from 'vue-count-to'
  import PanelGroup from '@/views/dashboard/PanelGroup'
  import LineChart from '@/views/dashboard/LineChart'

  const date = new Date();
  export default {
    name:"orderMenu",
    components: {
      PanelGroup,
      LineChart,
      CountTo
    },
    data() {
      this.chartSettings = {
        area: true
      };
      return {
        // 总利润
        totalProfit:0,
        // 总订单数量
        orderTotalNum:0,
        orderNum:[],
        profit:[],
        lineChartData: {
          columns: ['create_time','orderNum','profit'],
          rows: []
        },
        currentMonth:date.getFullYear()+"-"+(date.getMonth()+1)
      }
    },
    mounted(){
      this.getOrderStatInfo();
      this.getOrderChartInfo(this.currentMonth);
    },
    methods:{
      getOrderStatInfo() {
        getOrderStat().then(res=>{
          this.totalProfit = res.data.total_profit;
          this.orderTotalNum = res.data.total_order_count;
        })
      },
      convertValue(val) {
        if (val === '' || val === null) return;
        let arr = val.split("-");
        const year = arr[0].substring(2,5).padStart(2,'0');
        const month = arr[1].padStart(2,'0');
        return year+month;
      },
      getOrderChartInfo(query) {
        const val = this.convertValue(query);
        getOrderChartData(val).then(res=>{
          const date = new Date();
          const year = date.getFullYear();
          const month = date.getMonth();
          // 让X轴的长度变成当前月份的天数
          const numberOfDay = new Date(year,month,0).getDate();
          this.lineChartData.rows = new Array(numberOfDay);
            res.data.forEach(e=>{
              // 获取当前是第几天
              const day = new Date(e.create_time).getDate();
              // 修改属性值
              const obj = {'create_time':day,'orderNum':e.orderNum,'profit':e.profit};
              this.lineChartData.rows[day] = obj;
            });
          let index = -1;
          for (let rowsKey of this.lineChartData.rows) {
            index += 1;
            if (rowsKey === undefined) {
              this.lineChartData.rows[index] = {'create_time':index,'orderNum':0,'profit':0};
            }
          }
        })
      },
      // 查询当月订单
      queryMonthOrder(val) {
        this.getOrderChartInfo(val);
      }
    },
  }
</script>

<style lang="scss" scoped>
  .panel-group {
    margin-top: 18px;

    .card-panel-col {
      margin-bottom: 32px;
    }

    .card-panel {
      height: 108px;
      cursor: pointer;
      font-size: 12px;
      position: relative;
      overflow: hidden;
      color: #666;
      background: #fff;
      box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
      border-color: rgba(0, 0, 0, .05);

      &:hover {
        .card-panel-icon-wrapper {
          color: #fff;
        }

        .icon-people {
          background: #40c9c6;
        }

        .icon-message {
          background: #36a3f7;
        }

        .icon-money {
          background: #f4516c;
        }

        .icon-shopping {
          background: #34bfa3
        }
      }

      .icon-people {
        color: #40c9c6;
      }

      .icon-message {
        color: #36a3f7;
      }

      .icon-money {
        color: #f4516c;
      }

      .icon-shopping {
        color: #34bfa3
      }

      .card-panel-icon-wrapper {
        float: left;
        margin: 14px 0 0 14px;
        padding: 16px;
        transition: all 0.38s ease-out;
        border-radius: 6px;
      }

      .card-panel-icon {
        float: left;
        font-size: 48px;
      }

      .card-panel-description {
        float: right;
        font-weight: bold;
        margin: 26px;
        margin-left: 0px;

        .card-panel-text {
          line-height: 18px;
          color: rgba(0, 0, 0, 0.45);
          font-size: 16px;
          margin-bottom: 12px;
        }

        .card-panel-num {
          font-size: 20px;
        }
      }
    }
  }

  @media (max-width:550px) {
    .card-panel-description {
      display: none;
    }

    .card-panel-icon-wrapper {
      float: none !important;
      width: 100%;
      height: 100%;
      margin: 0 !important;

      .svg-icon {
        display: block;
        margin: 14px auto !important;
        float: none !important;
      }
    }
  }
</style>
